<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

	class Admin extends Controller {
		
		function Admin()
		{
			parent::Controller();
			
			$this->load->library('administration');
			$this->template['module']	= 'album';	
			$this->template['admin']		= true;
			$this->load->model('album_model', 'album');
		}
		
		function index()
		{
			//album list
			
			$this->lisitra();
		}
		
		function settings($storage = null)
		{
			if (is_null($storage))
			{
				if ($post = $this->input->post('submit') )
				{
					$fields = array('storage', 'album_col_num', 'user_can_create', 'show_meta');
					
					foreach ($fields as $field)
					{
						if ( $this->input->post($field) !== false)
						{
							$this->album->set($field, $this->input->post($field));
						}
					}
					$this->session->set_flashdata('notification', __("Settings updated", $this->template['module']));	
					redirect('admin/album/settings');
				}
				else
				{

				
					$this->load->helper('directory');
					$this->template['storages'] = directory_map('application/modules/album/libraries');
					

					$this->layout->load($this->template, 'admin/settings');
				
				}
				
				
			}
			else
			{
				$this->load->library($storage);
				$fields = $this->$storage->fields;
				$field_inputs = array();
				$this->template['fields'] = $fields;
				foreach ($fields as $field)
				{
					$field_inputs[] = $field['input'];
				}
				$this->user->check_level($this->template['module'], LEVEL_DEL);
				if ($post = $this->input->post('submit') )
				{
					foreach ($field_inputs as $field)
					{
						if ( $this->input->post($field) !== false)
						{
							$this->album->set($field, $this->input->post($field));
						}
					}
					$this->session->set_flashdata('notification', __("Settings updated", $this->template['module']));	
					redirect('admin/album/settings/' . $storage);
				}
				else
				{
					$this->template['storage'] = $storage;
					$this->layout->load($this->template, 'admin/storage');
				}
			}
			
		}
		
		function lisitra($search_id = null, $start = 0)
		{

			$params = array();
			$this->user->check_level($this->template['module'], LEVEL_VIEW);
			
			//sorting
			
			if ($search_id != 0 && $tmp = $this->album->get_tadiavo($search_id))
			{
				$params = unserialize( $tmp);
				//protection against curious

			}

			
			$search_id = $this->album->save_tadiavo(serialize($params));
			
			$per_page = 20;
			$params['start'] = $start;
			
			$params['limit'] = $per_page;
			
			$this->template['rows'] = $this->album->get_albums($params);
			//echo $this->db->last_query();
			
			if(!isset($this->template['title']))  $this->template['title'] = __("Album list", "album");
			$config['first_link'] = __('First', 'album');
			$config['last_link'] = __('Last', 'album');
			$config['total_rows'] = $this->album->get_total_albums($params);
			$config['per_page'] = $per_page; 
			$config['base_url'] = base_url() . 'admin/album/lisitra/' . $search_id;
			$config['uri_segment'] = 5;
			$config['num_links'] = 20;

			$this->load->library('pagination');
			
			$this->pagination->initialize($config); 

			$this->template['pager'] = $this->pagination->create_links();		
			$this->template['start'] = $start;		
			$this->template['search_id'] = $search_id;		
			$this->template['total'] = $config['total_rows'];		
			$this->template['per_page'] = $config['per_page'];		
			$this->template['total_rows'] = $config['total_rows'];
			
			$this->layout->load($this->template, 'admin/albums');
			

		
		}
		
		function move($direction, $id, $search_id, $start)
		{

			if (!isset($direction) || !isset($id))
			{
				redirect('admin/album');
			}
			
			$this->album->move($direction, $id);
			$this->lisitra($search_id, $start);				
			
					
		}
		
		function create($aid = null)
		{
			$this->template['title'] = __("Create an album", $this->template['module']);
			
			$this->user->check_level('album', LEVEL_ADD) ;
			
			
			$this->template['tags'] = $this->album->get_tags(array('limit' => 40, 'order_by' => 'cnt DESC' ));
			
			
			if ($this->input->post('submit'))
			{
				if (!$this->input->post('title'))
				{
					$this->template['error'][] = __("Title is required.", "album");
					$this->template['row'] = $_POST;
					$this->layout->load($this->template, 'ampidiro');
					return;
				}
				
				if ($aid = $this->input->post('aid'))
				{
					$this->user->check_level('album', LEVEL_EDIT) ;
					$row = $this->album->get_album(array('where' => array('a.aid' => $aid)));

					$aid = $this->album->save_album();
					$this->session->set_flashdata('notification', __("Album updated", "album"));
					
					redirect("admin/album");
					
				}
				else
				{

					$aid = $this->album->save_album();
					$this->plugin->do_action('album_save_album', $aid);
					$this->session->set_flashdata('notification', __("Album saved", "album"));
					redirect("admin/album");
					
				}
			}
			
			

			$row = array();
			
			if ($last_post = $this->session->userdata("last_post"))
			{
				$this->template['row'] = $last_post;
				$this->session->set_userdata("last_post", array());
			}
			$this->template['groups'] = $this->user->get_group_list();
			if (!is_null($aid))
			{
				$this->template['title'] = __("Modify the album", "album");
				$row = $this->album->get_album(array('limit' => null, 'where' => array('a.aid' => $aid)));
				if($row['username'] != $this->user->username)
				{
					//check only if not the submitter
					$this->user->check_level($this->template['module'], LEVEL_EDIT);
				}
				$this->template['row'] = $row;
				$this->template['sary'] = $this->album->get_images(array('where' => array('album_images.aid' => $row['aid'])));
				$this->template['groups'] = $this->user->get_group_list(array('where' => array('g_owner' => $row['username'])));
			}
			

			$this->layout->load($this->template, 'admin/album/create');
		
		}
		
	}

?>